/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main.initiate;

import java.io.File;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import main.KBProcess.*;
import main.NER.Trie.NER_Candidate;
import main.Ontology.ElementOntologyFactory;

/**
 * 
 * @author Isaac Osesina
 */
public class UTEES_Index implements Cloneable, Serializable{

	public final static String TAG_START_INDICATOR = "<";
	public final static String TAG_END_INDICATOR = ">";
	public final static char PADChar = '@';
	public final static String DOCUMENT_START_PAD = tag("S");
	public final static String DOCUMENT_END_PAD = tag("E");
        public static String ELEMENT_PAD = "" + PADChar + PADChar;
	public Set<Integer> docsToSkip, docsToAccept;
    public static final String PROP_FILE =  "ResourcesInfo.properties";
	File kBFile, ontologyFile;
	UTEES_MatchTable documentOntology;
	List<Fragment> fragmentList;
	List<Element> entityList;
	List<Element> elementList;
	KnowledgeBase kb;

	ElementOntologyFactory elementOntologyFactory;

	public UTEES_Index(String kbFileName, String ontologyFileName,
			Set<Integer> docsToSkip, Set<Integer> docsToAccept,
			ElementOntologyFactory elementOntologyFactory) {
		kBFile = kbFileName != null ? new File(kbFileName): null;
		ontologyFile = ontologyFile != null ? new File(ontologyFileName):null;
		this.docsToSkip = docsToSkip;
		this.docsToAccept = docsToAccept;
		this.elementOntologyFactory = elementOntologyFactory;
		init();
	}

	public UTEES_Index(Set<Integer> docsToSkip, Set<Integer> docsToAccept, 
			ElementOntologyFactory elementOntologyFactory) {
		this.docsToSkip = docsToSkip;
		this.docsToAccept = docsToAccept;
		this.elementOntologyFactory = elementOntologyFactory;
		init();
	}

	public static String tag(String s) {
		return TAG_START_INDICATOR + s + TAG_END_INDICATOR;
	}

	public static String tag(int s) {
		return TAG_START_INDICATOR + s + TAG_END_INDICATOR;
	}

	private void init() {
	    this.kBFile = new File(Configuration.getSingleton().getKBFile());
	    this.ontologyFile = new File(Configuration.getSingleton().getOntologiesFile());
		if (kBFile != null && ontologyFile != null) {
			KBProcess_IndexHousekeeping();
		}
	}
        
        public static boolean isTaggedText(String in) {
            return in.indexOf(TAG_START_INDICATOR) > 0 ? in.indexOf(TAG_END_INDICATOR, in.indexOf(TAG_START_INDICATOR))>0 : false;
        }

	private void KBProcess_IndexHousekeeping() {
		documentOntology = new UTEES_MatchTable(ontologyFile);
		UTEES_ProcessKB kBProcessor = new UTEES_ProcessKB(kBFile,
				documentOntology, elementOntologyFactory);
		kBProcessor.processKB(docsToSkip, docsToAccept);
		getParsedKB(kBProcessor.getKB());
	}

	private void getParsedKB(KnowledgeBase kb) {
		this.kb = kb;
		fragmentList = kb.getFragmentList();
		entityList = kb.getEntityList();
		elementList = kb.getElementList();

		// StringBuilder docTextTagged_BeforeFragment =
		// kb.getDocTextTagged_BeforeFragment(20);
		// StringBuilder docTextTagged_AfterFragment =
		// kb.getDocTextTagged_AfterFragment(20);

		// for(FragmentImpl f:fragmentList)
		// System.out.println(f.toString1());
	}

	public UTEES_MatchTable getDocumentOntology() {
		return documentOntology;
	}

	public List<Element> getElementList() {
		return elementList;
	}

	public List<Element> getEntityList() {
		return entityList;
	}

	public List<Fragment> getFragmentList() {
		return fragmentList;
	}

	public KnowledgeBase getKB() {
		return kb;
	}

	public static boolean isCorrect(NER_Candidate candidate,
			List<Element> entityList) {
		for (Element e : entityList) {
			if (candidate.compareTo(e) == 0)
				return true;
		}
		return false;
	}

	public static List<NER_Candidate> isCorrect(
			List<NER_Candidate> candidateList, List<Element> entityList) {
		if (entityList != null) {
			for (NER_Candidate c : candidateList) {
				boolean correct = isCorrect(c, entityList);
				c.setIsCorrect(correct);
			}
		}
		return candidateList;
	}
}
